<?php

namespace Model;


class UserHandler{
	
	private $m_db = NULL;
        
    public function __construct(Database $db) {
            $this->m_db =$db;
    }
	
	//Hämtar en användare
	public function getUser($username) {
			
		$sql = "SELECT * FROM user WHERE m_username = ?";
		$stmt = $this->m_db->Prepare($sql);
		
		$stmt->bind_param("s", $username);
		
		$stmt->execute();
		
		if ($stmt->bind_result($id, $username, $password) == FALSE) {
             throw new \Exception($this->mysqli->error);
        }
           
		if ($stmt->fetch()) {
        	$ret = new User($id, $username, $password);
		} else {
            throw new \Exception("Det finns ingen användare");
		}
         
        $stmt->close();
            
        return $ret;
			
	}
	
	//Lägger till en film
	public function AddMovie($image, $name, $review, $rating, $userId, $username){
		
		$sql = "INSERT INTO movie(m_image, m_title, m_review, m_rating, m_userId, m_username) VALUES (?, ?, ?, ?, ?, ?) ";
		
		$stmt = $this->m_db->Prepare($sql);
		
		$stmt->bind_param("ssssis",$image, $name, $review, $rating, $userId, $username);
		
		$insertId = $this->m_db->RunInsertQuery($stmt);
		
		return true; 
	}
	
	//Kolla om filmen redan finns
	public function MovieExist($name){
			
		$sql = "SELECT m_title FROM movie WHERE m_title=?";
	  			
		$stmt = $this->m_db->Prepare($sql);
		
        $stmt -> bind_param("s", $name);
		
		$stmt -> execute();
            
		if ($stmt->bind_result($name) == FALSE) {
                throw new \Exception($this->mysqli->error);
        }

        while ($stmt->fetch()) {
                return true;
				$stmt -> close();
        } 
        return false;
        $stmt -> close();
			
	}
	
	
	
}
